import { AxiosError } from '../../node_modules/axios/index';

interface ToastType {
    SHORT: number;
    LONG: number;
    show(content: string, duration?: number | undefined, mask?: boolean | undefined): void;
    info(
        content: string, duration?: number | undefined, 
        onClose?: (() => void) | undefined, mask ?: boolean | undefined): void;
    success(
        content: string, duration?: number | undefined, 
        onClose?: (() => void) | undefined, mask ?: boolean | undefined): void;
    fail(
        content: string, duration?: number | undefined, onClose?: (() => void) | undefined, mask?: boolean | undefined
    ): void;
    offline(
        content: string, duration?: number | undefined, 
        onClose?: (() => void) | undefined, mask?: boolean | undefined): void;
    loading(
        content: string, duration?: number | undefined, 
        onClose?: (() => void) | undefined, mask?: boolean | undefined): void;
    hide(): void;
}

const handleHttpError = (error: AxiosError, Toast: ToastType) => {
    let httpStatusCode: number = error.response!.status;
    switch (httpStatusCode) {
        case 500:
            Toast.fail('Http 接口错误(500)', 500);
            break;
        case 404:
            Toast.fail( '请求的资源不存在(404)', 500);
            break;
        case 400:
            Toast.fail('请求无效(400)', 500);
            break;
        case 405:
            Toast.fail( '请求无效(405)', 500);
            break;
        default:
            break;
    }
    throw error;
};

export default handleHttpError;